/*
 * @Author: tomystery
 * @Date:   2017-09-08 09:37:42
 * @Last Modified by:   tomystery
 * @Last Modified time: 2017-09-26 13:17:54
 */
'use strict';
require('./index.css');
require('page/common/nav-simple/index.js');

var _user = require('service/user-service.js');
var _mm = require('util/mm.js');


//表单里的错误提示
var formError = {
    show: function(errMsg) {
        $('.error-item').show().find('.err-msg').text(errMsg);
    },
     hide : function(){
        $('.error-item').hide().find('.err-msg').text('');
    }
}

//page逻辑部分
var page = {
    init: function() {
        this.bindEvent();
    },

    bindEvent: function() {
        var _this = this;
        //对username进行异步验证
        $('#username').blur(function() {
            var username = $.trim($('#username').val());
            	//如果用户名为空，我们不做验证
            	if(!username){
            		return;
            	}

            //异步验证用户名是否存在
            _user.checkUsername(username, function(res) {
                formError.hide();
            }, function(errMsg) {
                formError.show(errMsg);
            })

        });
        //登录按钮的点击
        $('#submit').click(function() {
            _this.submit();
        });
        //如果按下回车也是可以提交的
        $('.user-content').keyup(function(e) {
            if (e.keycode === 13) {
                _this.submit();
            }
        })
    },
    //提交表单
    submit: function() {
        var formData = {
            username: $.trim($('#username').val()),
            password: $.trim($('#password').val()),
            passwordConfirm: $.trim($('#password-confirm').val()),
            phone: $.trim($('#phone').val()),
            email: $.trim($('#email').val()),
            question: $.trim($('#question').val()),
            answer: $.trim($('#answer').val())
        }
        //表单验证结果
        var validateResult = this.formValidate(formData);
        if (validateResult.status) {
            //提交
            _user.register(formData, function(res) {
                console.log('成功'+res);
                // window.location.href ='./result.html?type=register'
            }, function(errMsg) {
                console.log('errMsg'+errMsg);
                formError.show(errMsg);
            });
        } else {

            //错误提示
            formError.show(validateResult.msg)

        }
      
    },
    //表单字段的验证
    formValidate: function(formData) {
        var result = {
            status: false,
            msg: ''
        };
        if (!_mm.validate(formData.username, 'require')) {
            result.msg = '用户名不能为空';
            return result;
        }
        if (!_mm.validate(formData.password, 'require')) {
            result.msg = '密码不能为空';
            return result;
        }
        //验证密码长度
        if (formData.password.length<6) {
            result.msg = '密码长度不少于6位';
            return result;
        }
        //验证两次密码
        if (formData.password!== formData.passwordConfirm) {
            result.msg = '两次密码不一致';
            return result;
        }
        //验证手机号格式不正确
         if (!_mm.validate(formData.phone, 'phone')) {
            result.msg = '手机格式不正确';
            return result;
        }
        //验证邮箱格式不正确
         if (!_mm.validate(formData.email, 'email')) {
            result.msg = '邮箱格式不正确';
            return result;
        }
        //问题不能为空
         if (!_mm.validate(formData.question, 'require')) {
            result.msg = '密码提示问题不能为空';
            return result;
        }
        //答案不能为空
         if (!_mm.validate(formData.answer, 'require')) {
            result.msg = '密码提示问题答案不能为空';
            return result;
        }

        //通过验证，返回正确提示
        result.status = true;
        result.msg = "验证通过";
        return result;
    }

}
page.init();